﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Microsoft.Windows.UserControls.Core.Converters
{
    public class InvertedBooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var flag = false;
            if (value is Boolean)
                flag = (bool)value;
            else if (value is Nullable<Boolean>)
            {
                var nullableFlag = (Nullable<Boolean>)value;
                flag = (nullableFlag.HasValue ? nullableFlag.Value : false);
            }
            return (flag ? Visibility.Collapsed : Visibility.Visible);
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is Visibility))
                return false;
            return (Visibility)value == Visibility.Collapsed;
        }
    }
}